package com.gitee.sidihuo.platform.base;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.LinkedBlockingQueue;

@Slf4j
public class BaseConsumer {

    public void initConsume() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    consume();
                } catch (Throwable t) {
                    log.warn("producer-consumer consume failed ", t);
                }
                log.info("producer-consumer consume thread finished");
            }
        });
        thread.setName("producer-consumer");
        thread.start();
        log.info("producer-consumer initConsume finished");
    }

    public void consume() {
        LinkedBlockingQueue<Object> queues = BaseProducer.queues;
        while (true) {
            try {
                Object take = queues.take();
                consumeHandler(take);
            } catch (Throwable e) {
                log.warn("producer-consumer consume while true failed ", e);
            }

        }
    }

    protected void consumeHandler(Object take) {
        log.info("producer-consumer consume take = {}", JSONObject.toJSONString(take));
    }

}
